package com.hypo.hystrixservice.command;

import com.alibaba.fastjson.JSONObject;
import com.hypo.hystrixservice.domain.UserInfo;
import com.hypo.hystrixservice.http.HttpClientUtils;
import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey;

/**
 * @Author: haibo.tang2
 * @Date: 2021/11/12
 * @Description:
 **/
public class GetUserInfoCommand extends HystrixCommand<UserInfo> {
    private String userId;
    public GetUserInfoCommand(String userId) {
        super(HystrixCommandGroupKey.Factory.asKey("GetUserInfoCommand"));
        this.userId=userId;
    }

    @Override
    protected UserInfo run() throws Exception {
        String url = "http://127.0.0.1:8082/user/getUserInfo?userId=" + userId;
        String response = HttpClientUtils.sendGetRequest(url);
        UserInfo userInfo = JSONObject.parseObject(response, UserInfo.class);
        return userInfo;
    }

    @Override
    protected String getCacheKey() {
        return "user_info_"+userId;
    }

    @Override
    protected UserInfo getFallback() {
        UserInfo userInfo = new UserInfo();
        userInfo.setId("1");
        return userInfo;
    }
}
